<template>
  <div>
    <!-- 搜索框 -->
    <div class="search">
      <p>商品类型搜索：</p>
      <div class="searchbox">
        <el-input v-model="input" placeholder="请输入"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="searchType"
          >搜索</el-button
        >
      </div>
    </div>
    <el-button
      type="primary"
      icon="el-icon-circle-plus-outline"
      class="add"
      @click="addNew"
      >新建</el-button
    >
    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column type="index" label="序号" width="180"> </el-table-column>
      <el-table-column prop="className" label="商品类型名称" width="750">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="text" class="left" @click="edit(scope.row)"
            >修改</el-button
          ><el-button type="text" class="right" @click="del(scope.row.classId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <div class="left">
        共<span>{{ msg.totalCount }}</span
        >条记录 第{{ msg.pageIndex }}/{{ msg.totalPage }}页
      </div>
      <div class="button">
        <el-button
          :disabled="pageIndex === 1"
          class="prev"
          @click="changeNum(-1)"
          >上一页</el-button
        ><el-button
          type="primary"
          class="next"
          @click="changeNum(1)"
          :disabled="pageIndex >= msg.totalPage"
          >下一页</el-button
        >
      </div>
    </div>
    <!-- 弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <el-form :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="className" label="商品类型名称">
          <el-input
            type="text"
            placeholder="请输入"
            v-model="form.className"
            maxlength="10"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="box">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addGoodsType">确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsType, getNewType, delGoods, editGoodsType } from '@/api/goods'
export default {
  name: 'goodsType',
  components: {},
  props: {},
  data() {
    return {
      // 搜索框绑定的输入
      input: '',
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      dialogVisible: false,
      title: '',
      msg: {},
      totalCount: 0,
      totalPage: '',
      classId: '',
      // 新增className
      form: {
        className: ''
      },
      rules: {
        className: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {},
  async created() {
    this.initData()
  },
  methods: {
    // 获取数据
    async initData() {
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      }
      const res = await getGoodsType(obj)
      console.log(res)
      this.tableData = res.currentPageRecords
      this.className = res.currentPageRecords.className
      this.totalCount = res.currentPageRecords.totalCount
      this.msg = res
    },
    // 搜索
    async searchType() {
      const obj = {
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        className: this.input
      }
      const res = await getGoodsType(obj)
      this.tableData = res.currentPageRecords
      this.input = ''
    },

    async addGoodsType() {
      this.dialogVisible = false
      // 新增
      if (!this.classId) {
        await getNewType(this.form)
      } else {
        // 修改
        await editGoodsType(this.classId, this.form)
      }
      this.initData()
      this.classId = ''
      this.form.className = ''
    },
    // 删除
    async del(id) {
      await delGoods(id)
      this.initData()
    },
    // 修改
    async edit(row) {
      this.dialogVisible = true
      this.title = '修改商品类型'
      console.log(row)
      // 修改不发请求 赋值
      // 不给id点击修改也是添加
      this.classId = row.classId
      this.form.className = row.className
      // console.log(res)
    },
    // 点击新建打开dialog
    addNew() {
      this.dialogVisible = true
      this.title = '新增商品类型'
    },
    // 上下页
    changeNum(num) {
      this.pageIndex += num
      this.initData()
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  display: flex;
  align-items: center;
  .el-input {
    width: 200px;
    margin: 20px;
    color: #6188fc;
  }
  .el-button {
    color: #fff;
    background-color: #6188fc;
    border: 0;
  }
}
.add {
  background-color: #fc6733;
  color: #fff;
  border: 0;
  margin-top: 50px;
  margin-bottom: 20px;
  &:hover {
    background-color: #fc6733;
  }
}
.left {
  color: #7093fc;
  font-size: 16px;
  font-weight: 700;
}
.right {
  color: #fd5c5e;
  font-size: 16px;
  font-weight: 700;
}
.footer {
  display: flex;
  align-items: center;
  .left {
    color: #dbdfe5;
    font-weight: 400;
    margin-top: 40px;
    margin-right: 800px;
  }
  .button {
    margin-top: 40px;
    .prev {
      background-color: #edf0f9;
    }
    .next {
      background-color: #d6def7;
      color: #606266;
      border: 0;
    }
  }
}
.box {
  text-align: center;
  .el-button:nth-child(1) {
    background-color: #fbf4f0;
    color: #606266;
    margin-right: 20px;
  }
  .el-button:nth-child(2) {
    background-color: #fc6834;
    color: #fff;
    border: 0;
  }
}
</style>
